package OrdersSystem;

import java.sql.Time;
import java.util.Date;
import java.util.Iterator;
import java.util.ArrayList;

import OrdersSystem.*;

/**
 * A class that represents an mock database with which the
 * orders system will communicate.
 * @author NVious
 *
 */
public class DataBase {
	
	// common variables for all the classes
	private static ArrayList<Order> orders;
	private static ArrayList<RegisteredUser> users;
	private static ArrayList<UserAlert> alerts;
	
	// constructor
	public DataBase() {
		orders = new ArrayList<Order>();
		users = new ArrayList<RegisteredUser>();
		alerts = new ArrayList<UserAlert>();
		populateData();
	}
	
	/********************************* accessors ******************************************************/
	
	// Order
	public Order retrieveOrderByOrderId(int orderID) {
		for(Order order : orders) {
	        if(order.getOrderID() == orderID){  //if the current order has this value, return it
	            return order; 
	        }
	    }
		return null;
	}
	
	public ArrayList<Order> retrieveOrderByCustomerID(int customerID) {
		ArrayList<Order> customerOrders = new ArrayList<Order>();
		for(Order order : orders) {
			if(order.getCustomerID() ==  customerID){  //if the current order has this value
	        	customerOrders.add(order);  // insert it into the list
	        }
	    }
		return customerOrders;		
	}
	
	public ArrayList<Order> retrieveAllOrders() { // used in historyTracking by employee
		return orders;
	}
	
	// Alert
	public UserAlert retrieveUserAlertByAlertId(int alertID) {
		for(UserAlert alert : alerts) {
			if(alert.getAlertID() == alertID){  //if the current alert has this value, return it
	            return alert; 
	        }
	    }
		return null;
	}
/*	
	public ArrayList<UserAlert> retrieveAlertsByUserID(int userID) {  // retrieves all the alerts for a particular user
		ArrayList<UserAlert> userAlerts = new ArrayList<UserAlert>();
		for(UserAlert alert : alerts) {
			if(alert.getUserID() ==  userID){  //if the current order has this value, return it
	        	userAlerts.add(alert); 
	        }
	    }
		return userAlerts;		
	}
*/	
	// Customer
	public RegisteredUser retrieveUser(int userID) {
		for(RegisteredUser user : users) {
			 if(user.getUserID() == userID){  //if the current alert has this value, return it
	            return user; 
	        }
	    }
		return null;
	}	
	

	
	// get Contact details
	public ContactDetails retrieveContactDetails(int userID) {
		for(RegisteredUser user : users) {
				if(user.getUserID() == userID) {
					return user.getAddress();
				}					
		}
		return null;
	}
	
	// other functions
	public int lastStoredOrderId() { // returns the id of the last stored Order: used in Order id generation
		
		int max = 0;
		for(int i = 0; i < orders.size(); i++) 
		{
			if (orders.get(i).getOrderID() > max) // if this element is bigger store it
				max = orders.get(i).getOrderID();
		}
		return max;
	}
	
	/********************************* mutators ******************************************************/
	
	// adding elements
	public void storeOrder(Order newOrder) {
		orders.add(newOrder);
	}	
	
	public void storeUser(RegisteredUser newUser) {
		users.add(newUser);
	}
	
	
	
	public void storeUserAlert(UserAlert newUserAlert) {
		alerts.add(newUserAlert);
	}
	
	// removing elements -- returns true for successful deletion or false for unsuccessful
	public boolean removeOrder(int orderID) {		
		for (Iterator<Order> iter = orders.iterator(); iter.hasNext(); ) {
			Order order = iter.next();  // get the object to which the iterator is pointing
			if (order.getOrderID() == orderID) {  // if it has this unique orderID, delete it from the list
				iter.remove();
				return true;
			}
		}
		return false;
	}	
	
	public void dropDatabase() { // deletes the elements from the database tables
		orders.clear();
		alerts.clear();
		users.clear();
	}

	
	/*
	public boolean removeAllOrders(int userID) {	// remove all the Orders of the particular Costumer
		boolean deleted = false;
		for (Iterator<Order> iter = orders.iterator(); iter.hasNext(); ) {
			Order order = iter.next();  // get the object to which the iterator is pointing
			if (order.getCustomerID() == userID) {  // if it has this unique orderID, delete it from the list
				iter.remove();
				deleted = true;
			}
		}
		return deleted;
	}
	*/
	/*
	public boolean removeCostumer(int userID) {
		for (Iterator<Customer> iter = customers.iterator(); iter.hasNext(); ) {
			Customer costumer = iter.next();  // get the object to which the iterator is pointing
			if (costumer.getUserID() == userID) {  // if it has this unique orderID, delete it from the list
				iter.remove();
				return true;
			}
		}
		return false;
	}
	*/ /*
	public boolean removeEmployee(int userID) {
		for (Iterator<PnGEmployee> iter = employees.iterator(); iter.hasNext(); ) {
			PnGEmployee employee = iter.next();  // get the object to which the iterator is pointing
			if (employee.getUserID() == userID) {  // if it has this unique orderID, delete it from the list
				iter.remove();
				return true;
			}
		}
		return false;
	}
	*/ /*
	public boolean removeUserAlert(int alertID) {
		for (Iterator<UserAlert> iter = alerts.iterator(); iter.hasNext(); ) {
			UserAlert alert = iter.next();  // get the object to which the iterator is pointing
			if (alert.getAlertID() == alertID) {  // if it has this unique orderID, delete it from the list
				iter.remove();
				return true;
			}
		}
		return false;
	}
	
	public boolean removeUserAlerts(int userID) {  // deletes all the Alerts of the user: userID
		boolean deleted = false;
		for (Iterator<UserAlert> iter = alerts.iterator(); iter.hasNext(); ) {
			UserAlert alert = iter.next();  // get the object to which the iterator is pointing
			if (alert.getUserID() == userID) {  // if it has this unique orderID, delete it from the list
				iter.remove();
                deleted = true;
			}
		}
		return deleted;
	}
	*/
	
	/********************************* private methods ******************************************************/
	
	private void populateData() 
	{
		// create a customer
		ContactDetails contact = new ContactDetails("Marchmont", "1F1", "Edinburgh", "EH9 1BM",
				"0798993454", "customer1@sms.ed.ac.uk");
		Customer customer = new Customer(111, "Eddie", "Murphy", contact, 0);
		this.storeUser(customer);
		
		// create a customer
		contact = new ContactDetails("Fountain Bridge", "3F2", "Edinburgh", "EH7 1AB",
				"0798991223", "customer1@sms.ed.ac.uk");
		customer = new Customer(112, "Adam", "Smith", contact, 0);
		this.storeUser(customer);
		
		// create an employee
		contact = new ContactDetails("Nicholson 12", "2F4", "Edinburgh", "EH8 1JK",
				"0745566754", "customer2@sms.ed.ac.uk");
		PnGEmployee employee = new PnGEmployee(113, "Sara", "Brightman", contact);
		this.storeUser(employee);
		
		Date date = null;//new Date();
		Time time = null;//new Time(564654);
		
		
		Order order = new Order(111, "gkjfhg", 2314, date, time, 21, 2, "ydykb");
		order.setOrderID(2);
		orders.add(order);
		
	}
	
}
	
	
